package com.yay.warehouse.tabs;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.yay.warehouse.IKeys;
import com.yay.warehouse.R;
import com.yay.warehouse.model.Ticket;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class TicketsAdapter extends BaseAdapter {
	public static boolean reload; 
	
	private static final long serialVersionUID = -5623354315863298693L;
	private static List<Ticket> tickets;
	private Context mContext;
    private Ticket ticket;
    private LinearLayout layout;
    private TableLayout table;
    private TextView name;
    private TextView date;
    private TextView description;
    private View separator;
    private LayoutParams lpFillParent = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    private LayoutParams lpLine = new LayoutParams(LayoutParams.FILL_PARENT, 1);
    private int light;
    private Drawable imgSeparator;
    private Date today = new Date();
	private int dividor = 1000*60*60*24; 
	private String S = " ";
	
	
    public TicketsAdapter(Context c) {
        this.mContext = c;
    }

    public static List<Ticket> getTickets() {
    	if(tickets == null) {
    		tickets = new ArrayList<Ticket>();
    	}
    	return tickets;
    }
    
    public int getCount() {
        return tickets.size();
    }

    public Ticket getItem(int position) {
        return tickets.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    	ticket = tickets.get(position);
    	
    	layout = new LinearLayout(mContext);
    	layout.setOrientation(LinearLayout.VERTICAL);
    	layout.setPadding(5, 0, 5, 5);
    		
        table = new TableLayout(mContext);
        table.setLayoutParams(lpFillParent);
        table.setStretchAllColumns(true);
        TableRow row = new TableRow(mContext);
    		
        name = new TextView(mContext);
        name.setText(ticket.getName());
        name.setTextColor(getLight());
        name.setTextSize(16f);
		row.addView(name);
			
		date = new TextView(mContext);
        date.setText(getDate(ticket.getDate()));
        date.setTextColor(getLight());
        date.setTextSize(12f);
        date.setGravity(Gravity.RIGHT);
		row.addView(date);
		table.addView(row);
		layout.addView(table);

		description = new TextView(mContext);
        description.setPadding(0, 5, 0, 5);
        description.setText(ticket.getDescription().length() > 30 ? ticket.getDescription().substring(0, 30) + "..." : ticket.getDescription());
        description.setTextColor(getLight());
        description.setTextSize(12f);
        layout.addView(description);
        
        separator = new View(mContext);
        separator.setLayoutParams(lpLine);
        separator.setBackgroundDrawable(getImgSeparator());
	    layout.addView(separator);
        return layout;
    }
    
    
    // PRIVATE METHODS
    
    private int getLight() {
    	if(light == 0) {
    		light = mContext.getResources().getColor(R.color.light);
    	}
    	return light;
    }
    
    private Drawable getImgSeparator() {
    	if(imgSeparator == null) {
    		imgSeparator = mContext.getResources().getDrawable(R.drawable.separator);
    	}
    	return imgSeparator;
    }

	private String getDate(String date) {
    	String ret="";
		try {
			Date d = IKeys.SDF.parse(date);
	    	int dif = (int)(Math.abs((today.getTime() - d.getTime())) / dividor);
			switch(dif) {
				case 0: ret = mContext.getResources().getString(R.string.today); break; 
				case 1: ret = mContext.getResources().getString(R.string.yesterday); break;
				case 2:	
				case 3:	
				case 4:	ret = dif + S + mContext.getResources().getString(R.string.dayAgo); break;
				default: ret = dif + S + mContext.getResources().getString(R.string.daysAgo); break; 	
			}
		} catch(ParseException e) {
        	Log.e(this.getClass().getName(), e.toString());
		}
		return ret;
    }

}
